﻿using CaptchaGen;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.Common;
using ZSZ.CommonMvc;
using ZSZ.Iservice;
using ZSZ.QianWeb.App_Start;

namespace ZSZ.QianWeb.Controllers
{
    public class UserController : Controller
    {
        public IUserService UserService { get; set; }
        public ISettingService SettingService { get; set; }
        // GET: User
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string name, string pass)
        {
            var user = UserService.GetByPhoneNum(name);
            if (user.LoginErrorTimes >= 5)
            {
                int shengMin = 30 - (DateTime.Now - user.LastLoginErrorDateTime).Value.Minutes;
                if (shengMin <= 30)
                {
                    return Json(new JsonCommon { Status = "err", ErrorMsg = "登录失败次数过多，您还有" + shengMin + "分钟可以重试" });
                }
                UserService.ResetError(user.Id);
            }
            if (!UserService.CheckLogin(name, pass))
            {
                UserService.AddError(user.Id);
                return Json(new JsonCommon { Status = "err", ErrorMsg = "登录失败" });
            }
            UserService.ResetError(user.Id);
            Session["UserId"] = user.Id;
            return Json(new JsonCommon { Status = "ok" });
        }
        public ActionResult CreateYan()
        {
            string yan = CommonHelper.GenerateCaptchaCode(4);
            MemoryStream ms = ImageFactory.GenerateImage(yan, 60, 100, 20, 6);
            TempData["ForgotYan"] = yan;
            return File(ms, "image/jpeg");
        }
        [HttpGet]
        public ActionResult ForgotPass1()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ForgotPass1([Required] string phone, [Required] string yan)
        {
            if (!ModelState.IsValid)
            {
                string err = ModelStateConsole.GetErrorMsg(ModelState);
                return Json(new JsonCommon { Status = "err", ErrorMsg = err });
            }
            string forgotYan = TempData["ForgotYan"].ToString();
            if (forgotYan != yan)
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "验证码不对" });
            }
            JiSuSmsSend send = new JiSuSmsSend();
            send.Appkey = SettingService.GetValue("急速数据appkey");
            string phoneNum = new Random().Next(1000, 9999).ToString();
            send.Content = SettingService.GetValue("急速数据content模板").Replace("@", phoneNum);
            JiSuSmsResult result = send.SendSms(phone);
            if (result.Status != "0")
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = result.Msg });
            }
            TempData["ForgotPhoneYan"] = phoneNum;
            TempData["Phone"] = phone;
            return Json(new JsonCommon { Status = "ok" });
        }
        [HttpGet]
        public ActionResult ForgotPass2() {
            return View();
        }
        [HttpPost]
        public ActionResult ForgotPass2(string yan) {
            if (yan != TempData["ForgotPhoneYan"].ToString())
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "手机验证码不对" });
            }
            return Json(new JsonCommon { Status="ok"});
        }
        [HttpGet]
        public ActionResult ForgotPass3() {
            return View();
        }
        [HttpPost]
        public ActionResult ForgotPass3(string pass,string pass2) {
            if (pass!=pass2)
            {
                return Json(new JsonCommon { Status="err",ErrorMsg="两次密码不一样"});
            }
            string phone = TempData["Phone"].ToString();
            var p = UserService.GetByPhoneNum(phone);
            UserService.UpdatePwd(p.Id,pass);
            return Json(new JsonCommon { Status="ok"});
        }
        [HttpGet]
        public ActionResult ForgotPass4() {
            return View();
        }
    }
}